package com.shorturl.controller;

import com.shorturl.pojo.Url;
import com.shorturl.service.UrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/url/")
@CrossOrigin
public class UrlController {

    @Autowired
    private UrlService urlService;


    @RequestMapping(value = "encode.do",method = RequestMethod.GET)
    @ResponseBody
    public String encode (String url){

        System.out.println(url);
        String sUrl = urlService.add(url);

        if(sUrl != null){
            return sUrl;
        }

        return "操作错误！！！";
    }

    @RequestMapping(value = "decode/{sUrl}",method = RequestMethod.GET)
    @ResponseBody
    public String decode (@PathVariable String sUrl){

        String url = urlService.selecrt(sUrl);

        if(url != null){
            return url;
        }
        return "操作错误！！！";
    }

}
